package com.zy.controller;

import com.zy.entity.Video;
import com.zy.entity.VideoText;
import com.zy.entity.VideoType;
import com.zy.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个人信息管理
 * @author 张宇
 * @date 2021/7/30 15:12
 */
@Controller
public class HomeController
{

    @Autowired
    private HomeService homeService;

    /**
     * 视频类型通过父id查询
     * @param parentId
     * @return
     */
    @ResponseBody
    @GetMapping("/select")
    public List<VideoType> select(@PathParam("parentId")int parentId)
    {
        return homeService.findVideoListByParentId(parentId);
    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("/upFile")
    public Map<String,Object> upFile(MultipartFile file)
    {
        Map<String,Object> map = new HashMap<>();
        String upFileMessage = homeService.upFile(file);
        if (upFileMessage == "上传失败")
        {
            map.put("upFileMessage","");
            map.put("message","上传失败!");
            map.put("code",104);
        }
        else
        {
            map.put("upFileMessage",upFileMessage);
            map.put("message","上传成功!");
            map.put("code",100);
        }
        return map;
    }

    /**
     * 添加视频信息
     * @param videoTitle
     * @param videoSrc
     * @param imgSrc
     * @param videoType
     * @param userId
     * @param explain
     */
    @ResponseBody
    @GetMapping("/addVideo")
    public void addVideo(@PathParam("videoTitle") String videoTitle,@PathParam("videoSrc")String videoSrc,
                         @PathParam("imgSrc") String imgSrc,@PathParam("videoType") int videoType,
                         @PathParam("userId") int userId,@PathParam("explain") String explain)
    {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        homeService.addVideo(new Video(null, videoTitle, videoSrc, imgSrc, videoType, userId, explain, simpleDateFormat.format(date),0,"1"));
    }

    /**
     * 得到视频详情
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/getVideoDetails")
    public VideoText getVideoDetails(int id)
    {
        VideoText videoText = homeService.findVideoById(id);
        homeService.addVideoViewById(videoText.getId(),videoText.getVideoView()+1);
        return homeService.findVideoById(id);
    }

    /**
     * 得到自己的视频列表
     * @param userId
     * @return
     */
    @ResponseBody
    @GetMapping("/getOneselfVideoList")
    public Map<String,Object> getOneselfVideoList(int userId, int pageNo, int pageSize)
    {
        return homeService.getOneselfVideoList(userId, pageNo, pageSize);
    }

}
